<?php

namespace app\api\controller\product;


use app\api\controller\Api;

class Spec extends Api
{


    public function create()
    {
        $post = $this->request->post();
        $id = $post['id'];
        $type = $post['type'];

        // $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
        $attr = $post['attrs'];
        $details = attr_format($attr)[1];
        $attrData = [];
        $count = 0;
        foreach ($details as $key => $item) {
            $detail = $item['detail'];
            foreach ($detail as $v => $d) {
                $detail[$v] = trim($d);
            }
            $suk = implode(',', $detail);
            $types = 1;

            if ($id) {
                $sukValue = [];
                /*
                $sukValue = $storeProductAttrValueServices->getColumn(['product_id' => $id, 'type' => 0, 'suk' => $suk], 'bar_code,cost,price,ot_price,stock,image as pic,weight,volume,brokerage,brokerage_two,vip_price', 'suk');
                if (!$sukValue) {
                    if ($type == 0) $types = 0; //编辑商品时，将没有规格的数据不生成默认值
                    $sukValue[$suk]['pic'] = '';
                    $sukValue[$suk]['price'] = 0;
                    $sukValue[$suk]['cost'] = 0;
                    $sukValue[$suk]['ot_price'] = 0;
                    $sukValue[$suk]['stock'] = 0;
                    $sukValue[$suk]['bar_code'] = '';
                    $sukValue[$suk]['weight'] = 0;
                    $sukValue[$suk]['volume'] = 0;
                    $sukValue[$suk]['brokerage'] = 0;
                    $sukValue[$suk]['brokerage_two'] = 0;
                }
                */
            } else {
                $sukValue[$suk]['pic'] = '';
                $sukValue[$suk]['price'] = 0;
                $sukValue[$suk]['cost'] = 0;
                $sukValue[$suk]['ot_price'] = 0;
                $sukValue[$suk]['stock'] = 0;
                $sukValue[$suk]['bar_code'] = '';
                $sukValue[$suk]['weight'] = 0;
                $sukValue[$suk]['volume'] = 0;
                $sukValue[$suk]['brokerage'] = 0;
                $sukValue[$suk]['brokerage_two'] = 0;
            }

            if ($types) {
                //编辑商品时，将没有规格的数据不生成默认值
                foreach (array_keys($detail) as $k => $title) {
                    $header[$k]['title'] = $title;
                    $header[$k]['align'] = 'center';
                    $header[$k]['minWidth'] = 130;
                }

                $index = 0;
                foreach ($detail as $k => $v) {
                    $attrData[$count]['value' . ($index + 1)] = $v;
                    $index++;
                }
                $attrData[$count]['detail'] = $detail;
                $attrData[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
                $attrData[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
                $attrData[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
                $attrData[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
                $attrData[$count]['vip_price'] = isset($sukValue[$suk]['vip_price']) ? floatval($sukValue[$suk]['vip_price']) : 0;
                $attrData[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
                $attrData[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
                $attrData[$count]['weight'] = floatval($sukValue[$suk]['weight']) ?? 0;
                $attrData[$count]['volume'] = floatval($sukValue[$suk]['volume']) ?? 0;
                $attrData[$count]['brokerage'] = floatval($sukValue[$suk]['brokerage']) ?? 0;
                $attrData[$count]['brokerage_two'] = floatval($sukValue[$suk]['brokerage_two']) ?? 0;
                $count++;
            }
        }

        $result = [];
        $result['attrData'] = $attrData;
        $result['attrs'] = array_keys($details[0]['detail']);

        return success($result);
    }
}